 export const baseUrl  = 'https://td.lszbg.com'
// export const baseUrl  = 'https://td.test.lszbg.cn'

const requestSettings = (opt) => {
    opt.url = baseUrl + opt.url
    return opt
}
const getSuccessInfo = async (response, data, resolve, reject) => {

    let res = response.data
    if (res.code == 200 && res.status == 1) {
        resolve(response)
    } else if (res.code === 200 && res.status === -1) {
        uni.removeStorageSync('userinfo');
        uni.removeStorageSync('token');
        uni.removeStorageSync('wechat_login_tag');

        //#ifdef APP-PLUS
        uni.navigateTo({
            url: '@/pages/logon/logon'
        })
        //#endif

        // #ifdef MP-WEIXIN || H5
        // uni.navigateTo({
        // 	url: '/pages/login/login'
        // })
        // #endif

    } else {
        reject(response)
    }
}

const getFailInfo = (error) => {
    // 网络异常的数据判断
    uni.hideLoading()
    return error
}

const request = function(data) {
    return new Promise(function(resolve, reject) {
        uni.request(requestSettings({
            ...data,
            header: {
                'Content-Type': 'application/json',
                'authorization': uni.getStorageSync('token')
            },
            success: (res) => {
                getSuccessInfo(res, data, (res) => {
                    resolve(res)
                }, (err) => {
                    reject(err)
                })
            },
            fail: (err) => {
                reject(getFailInfo(err))
            }
        }))
    })
}

export default request



